package com.baixun.gassystem.service.impl;

import com.baixun.gascommon.enums.ResultStatus;
import com.baixun.gascommon.utils.R;
import com.baixun.gassystem.service.SysFileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
* @author 智少
* @description 针对表【sys_authority】的数据库操作Service实现
* @createDate 2024-09-27 09:33:25
*/
@Service
public class SysFileServiceImpl implements SysFileService {

    @Value("${minio.bucket}")
    public String bucketName;


    @Value("${minio.showUrl}")
    public String showUrl;

    @Autowired
    private MinioClient minioClient;



    @Override
    public R upload(MultipartFile multipartFile) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {


        try {
            // 上传文件
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(multipartFile.getOriginalFilename()) // 使用文件原始名称作为对象名称
                    .stream(multipartFile.getInputStream(), multipartFile.getSize(), -1) // 文件流
                    .contentType(multipartFile.getContentType()) // 文件类型
                    .build());
            return R.success(showUrl+multipartFile.getOriginalFilename());
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(ResultStatus.NoUpFile);
        }
   }

    @Override
    public R okhttp() throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        // 构建请求对象，指定请求的URL和请求方法（此处为GET请求）
        Request request = new Request.Builder()
                .url("http://localhost:9988/user/info")
                .addHeader("Authorization","eyJ0eXAiOiJKV1QiLCJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJ1c2VySW5mbyI6IntcImlkXCI6MSxcInVzZXJOYW1lXCI6XCJhZG1pblwiLFwicGFzc1dvcmRcIjpcImRmMTBlZjg1MDlkYzE3NmQ3MzNkNTk1NDllN2RiZmFmXCIsXCJwaG9uZVwiOm51bGwsXCJzYWx0XCI6XCJhYmNcIixcImNyZWF0ZVRpbWVcIjpcIjIwMjQtMDktMjZUMTg6MDE6MjMuMDAwKzAwOjAwXCIsXCJzdGF0dXNcIjowLFwiZGVsZXRlZFwiOjB9IiwiaXNzIjoiU2hhbmdIYWlHdXMiLCJleHAiOjE3Mjg0NTkwODAsImF1dGhMaXN0IjpbInN5czp1cGRhdGUiLCJzeXM6dXBkYXRlIiwic3lzOnVwZGF0ZSIsInN5czp1cGRhdGUiLCJzeXM6dXBkYXRlIl0sImlhdCI6MTcyODQ0MTA4MH0.s-olJdf3PipC1GF9iIoentk2GhOLUrusQEo3Z7xnxbc")// 替换为你想请求的URL地址
                .build();
        Response execute = okHttpClient.newCall(request).execute();
        String string = execute.body().string();
        return R.success(string);



        //post请求
//        // 构建登录请求的数据
//        String json = "{\"userName\":\"yourUsername\", \"passWord\":\"yourPassword\"}"; // 替换为实际用户信息
//
//        // 创建请求体
//        RequestBody body = RequestBody.create(
//                json,
//                MediaType.parse("application/json; charset=utf-8")
//        );
//
//        // 构建请求
//        Request request = new Request.Builder()
//                .url(LOGIN_URL)
//                .post(body)
//                .build();

    }
}




